package com.irdstudio.efp.esb.api.rest.basicfn;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.api.bean.basicfn.req.RecRscWarnLoanReqBean;
import com.irdstudio.efp.esb.api.common.EsbRespServiceHandler;
import com.irdstudio.efp.esb.api.common.GroupeInterface;
import com.irdstudio.efp.esb.api.rest.EsbController;
import com.irdstudio.efp.esb.api.service.basicfn.RecRscWarnLoanService;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 需求编号：【】（业务提的需求编号）
 * 问题编号：【】（若不是因为修复问题而新增该类，则留空）
 * 开发人员：【】
 * 创建日期：【2019/11/19 9:16】
 * 功能描述：本服务用于风险决策平台把风险预警结果信息回调给网贷
 */
@RestController
@RequestMapping("esb")
public class RecRscWarnLoanController implements EsbController {
    /** 日志信息*/
    private final Logger logger = LoggerFactory.getLogger(RecRscWarnLoanController.class);
    @PostMapping("/SERVICE_40120009_01")
    public EsbRespServiceBean doRecv(@Validated @ModelAttribute("esbReqServiceBean") EsbReqServiceBean esbReqServiceBean, BindingResult bindingResult1,
                                     @ModelAttribute("recRscWarnLoanReqBean") RecRscWarnLoanReqBean recRscWarnLoanReqBean, BindingResult bindingResult2, Exception exception){
        try {
            // 参数校验
            validateBean(esbReqServiceBean, bindingResult1, recRscWarnLoanReqBean, bindingResult2, exception,logger);
            // 全局关联流水号
            String glblSrlNo = esbReqServiceBean.getSysHead().getGlblSrlNo();
            logger.info("======>风险决策结果通知接口【40120009_01】开始<======");
            // 设置系统头对象
            setSysHeadFieldThreadLocal(esbReqServiceBean);
            // 执行并返回
            return recRscWarnLoanService.handleWithDatas(recRscWarnLoanReqBean,esbReqServiceBean);
        }catch (ESBException e){
            return EsbRespServiceHandler.esbFailureHandler(esbReqServiceBean,e.getErrorCode(),e.getMessage());
        }finally{
            logger.info("======>风险决策结果通知接口【40120009_01】结束<======");
        }
    }

    @ModelAttribute("esbReqServiceBean")
    public EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
        logger.info("获取 ESB请求系统头和请求应用头对象!");
        EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
        return esbReqServiceBean;
    }
    @ModelAttribute("recRscWarnLoanReqBean")
    public RecRscWarnLoanReqBean getReqEsbBodyBean(HttpServletRequest request) {
        logger.info("获取 ESB请求体！");
        String esbReqBody = (String) request.getAttribute("esbReqBody");
        return JSONObject.parseObject(esbReqBody, RecRscWarnLoanReqBean.class);
    }

    @Autowired
    private RecRscWarnLoanService recRscWarnLoanService;
}
